﻿package export.garden
{
	import com.dusk.util.TextUtil;
	import com.game.manager.NotificationManager;
	
	import config.Config;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public dynamic class WorshipFlower extends MovieClip
	{
		public function WorshipFlower()
		{
			super();
			TextUtil.redrawTxtOld(this, ["info", "needNum", "current"]);
			this.addEventListener(Event.ADDED_TO_STAGE, added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, removed, false, 0, true);
		}
		
		public var atk:MovieClip;
		public var crit:MovieClip;
		public var def:MovieClip;
		public var emp:MovieClip;
		public var hit:MovieClip;
		public var hp:MovieClip;
		//----------------------------
		public var miss:MovieClip;
		//花魂技能图标
		public var skillmc:MovieClip;
		//花魂介绍
		public var info:TextField;
		//所需要的物品图标
		public var needMaterial:MovieClip;
		//升级所需
		public var needNum:TextField;
		//当前效果
		public var current:TextField;
		//返回按钮
		public var xBtn:SimpleButton;
		//供奉按钮
		public var doWorshipBtn:SimpleButton;
		private var curFlower:String = "";
		private var list:Array = ["atk", "crit", "def", "emp", "hit", "hp", "miss"];
		private var worshipList:Array = [
			"<font color=\'#FFFF00\'>千步连翘</font>",
			"<font color=\'#AA00FF\'>狂暴朝颜</font>",
			"<font color=\'#FF9933\'>抵抗皇菊</font>",
			"<font color=\'#0099FF\'>幽池青莲</font>",
			"<font color=\'#00FFFF\'>深谷香兰</font>", //6666FF
			"<font color=\'#FF0000\'>血海玫瑰</font>",
			"<font color=\'#00FF00\'>隐逸雏菊</font>"];
		private var allInfos:Array = ["\"千步连翘\" 花魂,供奉后可在攻击时概率添加破甲buff",
				"\"狂暴朝颜\" 花魂,供奉后可将暴击值转化为攻击值",
				"\"抵抗皇菊\" 花魂,供奉后可被动提升人物免伤",
				"\"幽池青莲\" 花魂,供奉后可在低蓝量时获得回蓝加成",
				"\"深谷香兰\" 花魂,供奉后可被动提升人物命中",
				"\"血海玫瑰\" 花魂,供奉后可在低血量时获得攻击加成",
				"\"隐逸雏菊\" 花魂,供奉后可在闪避时概率反弹伤害"];
		
		private function resetAllChild():void
		{
			for each(var i:String in this.list)
			{
				this[i].gotoAndStop(1);
			}
			this.info.text = "";
			this.current.text = "";
			this.skillmc.gotoAndStop(1);
			this.needMaterial.gotoAndStop(1);
			this.needNum.text = "";
		}
		
		private function updateAllInfo(param1:String):void
		{
			this.updateFlowerInfo(param1);
			this.updateCurrentInfo(param1);
		}
		
		private function updateFlowerInfo(param1:String):void
		{
			var i:String;
			for (i in this.list)
			{
				if (param1 == this.list[i] as String)
				{
					this.info.htmlText = this.allInfos[i];
					this.needNum.htmlText = this.worshipList[i] + " x ";
					break;
				}
			}
		}
		
		private function updateCurrentInfo(flowerName:String):void
		{
			var _loc1_:Object = Config.getIns().flowerSoul.getUpDataByType(flowerName);
			var _loc3_:String = "";
			if (_loc1_.type != flowerName)
			{
				NotificationManager.alert("花魂类型不符");
				return;
			}
			this.needNum.appendText(_loc1_.needNum);
			if (_loc1_.curLevel > 0)
			{
				(this.skillmc.getChildAt(0) as MovieClip).gotoAndStop(2);
			}
			else
			{
				(this.skillmc.getChildAt(0) as MovieClip).gotoAndStop(1);
			}
			var curEffect:Object = _loc1_.curEffect;
			_loc3_ += "Lv." + _loc1_.curLevel;
			_loc3_ += _loc1_.curLevel >= Config.getIns().flowerSoul.MAX_SOUL_LEVEL ? ("(Max):\n") : ":\n";
			switch (_loc1_.type)
			{
				case "atk":
					_loc3_ += "攻击时有" + int(curEffect.odds * 100) + "%的概率增加破甲buff,持续" + curEffect.duration + "秒";
					break;
				case "crit":
					_loc3_ += "暴击值高于" + int(curEffect.treshold) + "%的部分将会转换为伤害值";
					break;
				case "def":
					_loc3_ += "被动增加人物" + Math.round((1 - curEffect.key) * 100) + "%的免伤";
					break;
				case "emp":
					_loc3_ += "魔法值低于" + int(curEffect.treshold * 100) + "%时,回蓝增加" + int((curEffect.rate - 1) * 100) + "%";
					break;
				case "hit":
					_loc3_ += "被动增加人物" + curEffect.key + "%的命中";
					break;
				case "hp":
					_loc3_ += "血量越低,伤害越高,最高增加" + int(curEffect.treshold * 100) + "%";
					break;
				case "miss":
					_loc3_ += "闪避时有" + int(curEffect.odds * 100) + "%的概率反弹" + curEffect.rate + "倍攻击值的伤害给伤害来源";
					break;
			}
			this.current.text = _loc3_;
		}
		
		private function added(param1:Event):void
		{
			for each(var item:String in this.list)
			{
				with (this[item] as MovieClip)
				{
					buttonMode = true;
					addEventListener(MouseEvent.CLICK, clickFlowerHandler);
				}
			}
			this.doWorshipBtn.addEventListener(MouseEvent.CLICK, doWorship);
			this.xBtn.addEventListener(MouseEvent.CLICK, close);
			resetAllChild();
			this.info.text = "神花吸收天地精华演化出花魂,收集神花供奉花魂可以获得无与伦比的力量"
		}
		
		private function removed(param1:Event):void
		{
			for each(var item:String in this.list)
			{
				with (this[item] as MovieClip)
				{
					buttonMode = true;
					removeEventListener(MouseEvent.CLICK, clickFlowerHandler);
				}
			}
			this.doWorshipBtn.removeEventListener(MouseEvent.CLICK, doWorship);
			this.xBtn.removeEventListener(MouseEvent.CLICK, close);
		}
		
		private function clickFlowerHandler(param1:MouseEvent):void
		{
			this.resetAllChild();
			this.curFlower = (param1.target as MovieClip).name;
			this[this.curFlower].gotoAndStop(2);
			this.skillmc.gotoAndStop(this.curFlower);
			this.needMaterial.gotoAndStop(this.curFlower);
			this.updateAllInfo(this.curFlower);
		}
		
		private function doWorship(param1:MouseEvent):void
		{
			if (this.curFlower == "")
			{
				NotificationManager.alert("请选择花魂");
				return;
			}
			var _loc1_:int = Config.getIns().playerData.getStuffNumByName("flower_" + this.curFlower);
			var _loc2_:int;
			var _loc3_:uint = Config.getIns().flowerSoul.getUpDataByType(this.curFlower).needNum;
			if (0 == _loc3_)
			{
				NotificationManager.alert("该花魂已达最高等级");
				return;
			}
			if (_loc1_ + _loc2_ < _loc3_)
			{
				NotificationManager.alert("供奉材料不足！还需" + (_loc3_ - _loc1_ - _loc2_) + "朵");
				return;
			}
			if (_loc1_ >= _loc3_)
			{
				Config.getIns().playerData.removeEquipFormBag("flower_" + this.curFlower, 2, _loc3_);
			}
			else
			{
				Config.getIns().playerData.removeEquipFormBag("flower_" + this.curFlower, 2, _loc1_);
			}
			var result:String = Config.getIns().flowerSoul.upgradeSoulLevelByType(this.curFlower);
			NotificationManager.alert(result);
			this.updateAllInfo(this.curFlower);
		}
		
		private function close(param1:MouseEvent):void
		{
			this.resetAllChild();
			if (this.parent)
			{
				parent.removeChild(this);
			}
		}
	}
}
